﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sweepids.Models.Dao;
using Sweepids.Models.Object;

namespace Sweepids.Controllers
{
    public class ManageItemsController : Controller
    {
        //
        // GET: /ManageItems/
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult AddAItem()
        {
            try
            {
                var catDao = new CategoryDao();
                var categories = catDao.ReadAll();
                return View(categories);
            }
            catch (Exception)
            {
                TempData["Error"] = "There was trouble viewing the items to add.";
                return RedirectToAction("Index", "ManageItems");
            }
        }
        public ActionResult AddCategory()
        {
            return View();
        }
        public ActionResult EditItem()
        {
            try
            {
                //retrieve itemDao object
                var itemDao = new ItemDao();
                //get list of all items 
                var items = itemDao.ReadAll();
                var itemsFinal = new List<Item>();
                //remove items from list that have expired in date as they cannot be edited from here out
                foreach (var item in items)
                {
                    if (item.EndTime > DateTime.Now)
                    {
                        itemsFinal.Add(item);
                    }
                }

                return View(itemsFinal);
            }
            catch (Exception)
            {
                TempData["Error"] = "There was trouble viewing the items to edit.";
                return RedirectToAction("Index", "ManageItems");
            }
        }
        public ActionResult DeleteItem()
        {
            try
            {
                //create item dao
                var itemDao = new ItemDao();
                //retrieve all items
                var items = itemDao.ReadAll();
                return View(items);
            }
            catch (Exception)
            {
                TempData["Error"] = "There was trouble viewing the items to delete.";
                return RedirectToAction("Index", "ManageItems");
            }
        }
        [HttpPost]
        public ActionResult DeleteAItem(string items)
        {
            try
            {
                //get Dao
                var itemDao = new ItemDao();
                //create item with this id
                var item = new Item { Id = Convert.ToInt32(items) };
                //delete item
                itemDao.Delete(item);
                //need list of items to pass through
                var itemlist = itemDao.ReadAll();

                TempData["Success"] = "Successfully deleted the item.";
                return View("DeleteItem", itemlist);
            }
            catch (Exception)
            {
                TempData["Error"] = "This item was already bid on, can't delete.";
                return RedirectToAction("Index", "ManageItems");
            }
        }
        [HttpPost]
        public ActionResult EditAItem(string items)
        {
            try
            {
                //create itemdao object
                var itemDao = new ItemDao();
                //retrieve the specific item object from the database
                var theItem = itemDao.Read(Convert.ToDecimal(items));
                //calculate percentage
                var percent = (int)((1 - (theItem.Price / theItem.OriginalPrice)) * (decimal)100.0);
                var viewModel = new EditViewModel { item = theItem, percent = percent };
                //get categories to pass through
                var catDao = new CategoryDao();
                var categories = catDao.ReadAll();
                viewModel.categories = categories;

                return View(viewModel);
            }
            catch (Exception)
            {
                TempData["Error"] = "There was trouble viewing the item to edit.";
                return RedirectToAction("Index", "ManageItems");
            }
        }
        [HttpPost]
        public ActionResult EditTheItem(string id, string name, string Oprice, string photo, string percentDiscount, string endTime, string description, string category)
        {
            try
            {
                //create daos
                var itemDao = new ItemDao();
                var catDao = new CategoryDao();
                //create new item with all the passed through attributes
                var cat = catDao.ReadByName(category);
                var theId = Convert.ToInt32(id);
                //get original item from database
                var myitem = itemDao.Read(theId);
                var originalPrice = Convert.ToDecimal(Oprice);
                var discount = Convert.ToDecimal(percentDiscount);
                var price = originalPrice - (originalPrice * (discount / (decimal)100.0));
                var end = DateTime.Parse(endTime);
                var item = new Item
                    {
                        Name = name,
                        Id = theId,
                        OriginalPrice = originalPrice,
                        Description = description,
                        EndTime = end,
                        StartTime = DateTime.Now,
                        Photo = photo,
                        Price = price,
                        SweepsOnItem = myitem.SweepsOnItem,
                        WinUser = myitem.WinUser,
                        CatId = cat.CatId
                    };
                itemDao.Update(item);
                var listItem = itemDao.ReadAll();
                var listItemFinal = new List<Item>();
                foreach (var aitem in listItem)
                {
                    if (aitem.EndTime > DateTime.Now)
                    {
                        listItemFinal.Add(aitem);
                    }
                }

                TempData["Success"] = "Successfully edited the item.";
                return View("EditItem", listItemFinal);
            }
            catch (Exception)
            {
                TempData["Error"] = "There was trouble editing the item.";
                return RedirectToAction("Index", "ManageItems");
            }
        }
        [HttpPost]
        public ActionResult AddItem(string name, string Oprice, string percentDiscount, string endTime,
                                    string description, string photo, string category)
        {

            try
            {
                var photoUrl = photo;
                var catDao = new CategoryDao();
                var itemDao = new ItemDao();
                var cat = catDao.ReadByName(category);
                var end = DateTime.Parse(endTime);
                var originalPrice = Convert.ToDecimal(Oprice);
                var discount = Convert.ToDecimal(percentDiscount);
                var price = originalPrice - (originalPrice * (discount / (decimal)100.0));
                var item = new Item
                    {
                        CatId = cat.CatId,
                        CategoryObj = cat,
                        Description = description,
                        OriginalPrice = originalPrice,
                        EndTime = end,
                        StartTime = DateTime.Now,
                        Name = name,
                        Photo = photoUrl,
                        Price = price,
                        SweepsOnItem = 0,
                        WinUser = ""
                    };
                itemDao.Create(item);
                var categories = catDao.ReadAll();

                TempData["Success"] = "Successfully added the item.";
                return View("AddAItem", categories);
            }
            catch (Exception)
            {
                TempData["Error"] = "There was trouble adding the item.";
                return RedirectToAction("Index", "ManageItems");
            }
        }

        [HttpPost]
        public ActionResult CategoryAdded(string name)
        {
            try
            {
                var categoryDao = new CategoryDao();
                var category = new Category { Name = name };
                categoryDao.Create(category);
                var catlist = categoryDao.ReadAll();

                TempData["Success"] = "Successfully created the category.";
                return View("AddAItem", catlist);
            }
            catch (Exception)
            {
                TempData["Error"] = "There was trouble creating the category.";
                return RedirectToAction("Index", "ManageItems");
            }
        }

    }
    public class EditViewModel
    {
        public Item item { get; set; }
        public int percent { get; set; }
        public List<Category> categories { get; set; }
    }
}
